<?php
/**
 * Process transaction data
 */
class TransactionController extends AppController {
	/**
	 * Change status(delete, un-delete, make success, make fault of transaction in admin
	 */
	function admin_update_status() {
		if ($this->RequestHandler->isPost()) {
			$value = 0;
			if (isset($this->data['value'])) {
				$value = $this->data['value'];
			}
			$type = 'delete';
			if (isset($this->data['type']) && $this->data['type'] === 'status') {
				$type = 'status';
			}
			$id = array();
			if (isset($this->data['id'])) {
				$id = $this->data['id'];
			}
			if ($type == 'delete') {
				$this->Transaction->updateDeleteFlg($id, $value);
			} else {
				$this->Transaction->updateStatusFlg($id, $value);
			}
			$this->redirect($this->referer());
		} else {
			$this->cakeError('error404');
		}
	}

	/**
	 * Display transaction is process by a user
	 *
	 * @param null $userId
	 */
	function profile($userId = null) {
		if (!isset($userId)) {
			$userId = $this->CustomAuth->user('id');
		}
		if (!$userId) {
			$this->cakeError('error404');
		}
		$this->paginate = $this->Transaction->getUserTransaction($userId, array(), true);
		$this->paginate['limit'] = 10;
		$transactions = $this->paginate('Transaction');
		$ids = array();
		foreach ($transactions as $transaction) {
			$ids[] = $transaction['Transaction']['detail_id'];
		}
		$this->loadModel('Course');
		$courses = $this->Course->getCourseByDetail($ids);
		$coursesDetail = array();
		foreach ($courses as $course) {
			$coursesDetail[$course['CourseDetail']['id']] = $course['Course'];
		}
		$this->set(array(
			'userId' => $userId,
			'transactions' => $transactions,
			'coursesDetail' => $coursesDetail,
		));
	}
}